import {AbilityModel} from "../../controller/model/AbilityModel";
import Configer from "../../frame/apiModel/config/Configer";
import {FinalKey} from "../../constant/FinalConstant";
import {UnitController} from "../../controller/UnitController";
import {Logger} from "../../frame/apiModel/config/Logger";
import {UnitModel} from "../../frame/apiModel/adapterModel/model3/UnitModel";
import {ItemModel} from "../../frame/apiModel/adapterModel/model3/ItemModel";
import {PlayerModel} from "../../frame/apiModel/adapterModel/model4/PlayerModel";
import {LocModel} from "../../frame/apiModel/base/LocModel";
import {PlayerFactory} from "../../frame/PlayerFactory";
import {DataRouterUtil} from "../../utils/router/DataRouterUtil";
import {CodeUtil} from "../../frame/codeAdapter/CodeUtil";
import {AttributeEnum, UnitType} from "../../frame/dataSource/DataModel";
import {Util} from "../../utils/Util";
import {AbilityUtil} from "./AbilityUtil";
import UnitUtil from "../../utils/UnitUtil";
import {DataRouter} from "../../frame/dataSource/DataRouter";
import {EffectModel} from "../../frame/apiModel/adapterModel/model2/EffectModel";
import {ModelFactory} from "../../frame/apiModel/adapterModel/model5/ModelFactory";
import {TimeControllerOpt} from "../../controller/model/ControllerOpt";
import {TimeController} from "../../controller/TimeController";
import MathUtil from "../../frame/codeAdapter/MathUtil";
import {GlobalEnv} from "../../utils/GlobalEnv";
import {ModelType} from "../../frame/apiModel/adapter/adapterInterface/model5/IModelFactory";

export default class PushSkillModel extends AbilityModel {
    abilityId: string = Configer.getFinal(FinalKey.pushSkill) as string;

    actionFun = (activeUnit: UnitModel, activeItem: ItemModel, targetUnit: UnitModel, targetItem: ItemModel, activePlayer: PlayerModel, targetPlayer: PlayerModel, spellId: string, spellLoc: LocModel)  => {
        let success;
        this.pushSkill(activeUnit, activePlayer);
        return success;
    }

    pushSkill (activeUnit: UnitModel, player: PlayerModel) {
        const range = 300;
        const distance = 30;
        const times = 5;
        const toSpeed = 0.05;
        const activeLoc = activeUnit.loc;

        const unitArr: UnitModel[] = [];
        ModelFactory.getInstance().mapUnitInRange(activeLoc, range,
            (unit) => {
                const isAlive = DataRouterUtil.checkUnitUseAttr(unit);
                if (isAlive) {
                } else {
                    return;
                }
                if (activeUnit === unit) {
                    return;
                }
                const isHero = GlobalEnv.isInHeroGroup(unit);
                if (isHero) {
                    unitArr.push(unit);
                }
                const tempPlayer = PlayerFactory.getInstance().getUnitOwner(unit)
                let checkCamp = !tempPlayer.isPlayerAlly(player);
                if (checkCamp) {
                    unitArr.push(unit);
                }
            }
        )

        let currentTime = 0;
        const callBack = () => {
            currentTime  = currentTime + 1;
            for (let i = 0; i < unitArr.length; i++) {
                const unit = unitArr[i];
                if (unit?.isAliveInBattle()) {
                    const unitLoc = unit.loc;
                    const angle = activeLoc.getAngle(unitLoc);
                    const unitX = unitLoc.x;
                    const unitY = unitLoc.y;
                    const newX = unitX + distance * MathUtil.cos(angle);
                    const newY = unitY + distance * MathUtil.sin(angle);
                    unit.loc = new LocModel(newX, newY);
                } else {
                    continue;
                }
            }
        }

        const timerOpt = new TimeControllerOpt(undefined, callBack, times);
        TimeController.getInstance().startTimer({time: toSpeed, opt: timerOpt});
    }
}